﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace btnet.pages
{
    public partial class svn_log : CommonPage
    {
        protected string file_path;
        protected int rev;
        protected string log;
        protected string repo;
        protected string string_affected_path_id;

        protected void Page_Load(object sender, EventArgs e)
        {
            Bug_Util.do_not_cache(Response);

            security = new Security();
            security.check_security(HttpContext.Current, Security.ANY_USER_OK);


            // get info about revision

            string sql = @"
select svnrev_revision, svnrev_repository, svnap_path, svnrev_bug
from svn_revisions
inner join svn_affected_paths on svnap_svnrev_id = svnrev_id
where svnap_id = $id
order by svnrev_revision desc, svnap_path";

            int svnap_id = Convert.ToInt32(Bug_Util.sanitize_integer(Request["id"]));
            string_affected_path_id = Convert.ToString(svnap_id);

            sql = sql.Replace("$id", string_affected_path_id);

            DataRow dr = DbUtil.get_datarow(sql);

            // check if user has permission for this bug
            int permission_level = Bug.get_bug_permission_level((int)dr["svnrev_bug"], security);
            if (permission_level == Security.PERMISSION_NONE)
            {
                Response.Write("You are not allowed to view this item");
                Response.End();
            }

            idlog.Value = string_affected_path_id;


            repo = (string)dr["svnrev_repository"];
            file_path = (string)dr["svnap_path"];
            rev = (int)dr["svnrev_revision"];


            log = VersionControl.svn_log(repo, file_path, rev);

            if (log.StartsWith("ERROR:"))
            {
                Response.Write(log);
                Response.End();
            }

        }


        ///////////////////////////////////////////////////////////////////////
        void fetch_and_write_history(string file_path)
        {

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(log);
            XmlNode log_node = doc.ChildNodes[1];
            string file_path_with_leading_slash = "/" + file_path;

            int row = 0;
            foreach (XmlElement logentry in log_node)
            {

                string revision = logentry.GetAttribute("revision");
                string author = "";
                string date = "";
                string path = "";
                string action = "";
                //string copy_from = "";
                //string copy_from_rev = "";
                string msg = "";

                foreach (XmlNode node in logentry.ChildNodes)
                {
                    if (node.Name == "author") author = node.InnerText;
                    else if (node.Name == "date") date = Bug_Util.format_db_date_and_time(XmlConvert.ToDateTime(node.InnerText, XmlDateTimeSerializationMode.Local));
                    else if (node.Name == "msg") msg = node.InnerText;
                    else if (node.Name == "paths")
                    {
                        foreach (XmlNode path_node in node.ChildNodes)
                        {
                            if (path_node.InnerText == file_path_with_leading_slash)
                            {
                                XmlElement path_el = (XmlElement)path_node;
                                action = path_el.GetAttribute("action");
                                if (!action.Contains("D"))
                                {
                                    path = path_node.InnerText;

                                    if (path_el.GetAttribute("copyfrom-path") != "")
                                    {
                                        file_path_with_leading_slash = path_el.GetAttribute("copyfrom-path");
                                    }
                                }
                            }
                        }
                    }
                }

                Response.Write("<tr><td class=datad>" + revision);
                Response.Write("<td class=datad>" + author);
                Response.Write("<td class=datad>" + date);
                Response.Write("<td class=datad>" + path);
                Response.Write("<td class=datad>" + action);
                //        Response.Write("<td class=datad>" + copy_from);
                //        Response.Write("<td class=datad>" + copy_from_rev);
                Response.Write("<td class=datad>" + msg.Replace(Environment.NewLine, "<br/>"));


                Response.Write("<td class=datad><a target=_blank href=svn_view.aspx?id=" + string_affected_path_id
                    + "&rev=" + revision
                    + ">");

                Response.Write("view</a>");

                Response.Write("<td class=datad><a target=_blank href=svn_blame.aspx?id=" + string_affected_path_id
                    + "&rev=" + revision
                    + ">");

                Response.Write("annotated</a>");

                Response.Write("<td class=datad><a id=" + revision
                    + " href='javascript:sel_for_diff("
                    + Convert.ToString(row)
                    + ",\""
                    + revision
                    + "\")'>select for diff</a>");


            }
        }

    }
}
